<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: downloads.php Sat Jul 18 12:40:38 CST 2009
 */
include "conf.inc";

$id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;

$attachment_handler = xoops_getmodulehandler("attachments");
$filepath = XOOPS_UPLOAD_PATH."/groups/";
$att_obj = $attachment_handler->get($id);

if ( empty($att_obj) || !is_object($att_obj) || $att_obj->isNew() ) { redirect_header("index.php");}

$filename = $att_obj->getVar("att_filename","n");
$att_obj->setVar("att_downloads",$att_obj->getVar("att_downloads") + 1 );
$attachment_handler->insert($att_obj);

$path = $filepath . $att_obj->getVar("att_attachments");

$ext = substr( $path, strrpos( $path, "." ) + 1 );
$types = include(dirname(__FILE__)."/include/mimetypes.inc.php");
$content_type = isset($types[$ext]) ? $types[$ext] : "text/plain";

header('Content-type: '.$content_type .'; charset='._CHARSET);
header('Cache-control: max-age=31536000'); 
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+31536000) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s' , filemtime($path) . ' GMT'));
header('Content-Encoding: none');
header('Content-Disposition: attachment; filename='.$filename );

$handle = fopen($path, "rb");
while (!feof($handle)) {
   $buffer = fread($handle, 4096);
   echo $buffer;
}
fclose($handle);
?>